Imports NHibernate
Imports KanBan.Pgd.Domain.Entities
Imports KanBan.Pgd.Domain.ValueObjects

Namespace Repositories

    ''' <summary>
    ''' Se encarga de la persistencia de los grupos
    ''' </summary>
    ''' <remarks></remarks>
    Public Class GroupRepository
        Inherits RepositoryBase(Of Group)

        <Obsolete()> _
        Public Sub AddUser(ByVal group As Group, ByVal user As User)

            Dim session As ISession = NHibernateHelper.GetCurrentSession()
            group.Users.Add(user)
            session.SaveOrUpdate(group)
            session.Flush()

        End Sub

        Public Sub AddPermission(ByVal group As Group, ByVal permission As Permission, Optional ByVal isDeletable As Boolean = False)

            Dim session As ISession = NHibernateHelper.GetCurrentSession()

            Dim gp As GroupPermission = GroupPermission.CreateGroupPermission(group, _
                                                         permission, _
                                                         isDeletable)
            group.GroupPermissions.Add(gp)
            session.SaveOrUpdate(gp)
            session.Flush()

        End Sub

        'ToDo: Validate this method, evaluate a better solution for handling these collections!
        Public Sub RemovePermission(ByVal group As Group, ByVal permission As Permission)

            Dim session As ISession = NHibernateHelper.GetCurrentSession()

            For Each gp As GroupPermission In group.GroupPermissions
                If gp.Permission.Id = permission.Id Then
                    'group.GroupPermissions.Remove(gp)
                    session.Delete(gp)
                    session.Flush()
                    Exit For
                End If
            Next

        End Sub

    End Class

End Namespace